# -*- coding: utf-8 -*-
 
import hashlib
from base import UserCase, Runner, TestSuite

class Case(UserCase):

    def test_user_message_list(self):
        res = self.c.open('/api/my-user-message/list')
        self.assertEqual(res['code'], 0)

    def test_user_message_create(self):
        p = {
            'title': self.randhan(8),
            'content': self.randhan(64),
        }
        res = self.c.open('/api/my-user-message/create', p)
        self.assertEqual(res['code'], 0)


    def test_user_message_delete(self):
        p = {
            'title': self.randhan(8),
            'content': self.randhan(64),
        }
        res = self.c.open('/api/my-user-message/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        p = {
            'title': self.randhan(8),
            'content': self.randhan(64),
        }
        res = self.c.open('/api/my-user-message/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/my-user-message/list', {'orderBy': 'user_message.id', 'orderDesc': '1'})
        self.assertEqual(res['data']['itemList'][0]['id'], id)

        self.c.open('/api/my-user-message/delete', {'id': id})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-user-message/list')
        self.assertNotEqual(res['data']['itemList'][0]['id'], id)


    def test_user_message_update(self):
        p = {
            'title': self.randhan(8),
            'content': self.randhan(64),
        }
        res = self.c.open('/api/my-user-message/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/my-user-message/read', {'id': id})
        self.assertEqual(res['data']['message_obj']['content'], p['content'])
        self.assertEqual(res['data']['status'], 'normal')

        p = {
            'id': id,
            'status': 'readed'
        }
        res = self.c.open('/api/my-user-message/update', p)
        self.assertEqual(res['code'], 0)
        res = self.c.open('/api/my-user-message/read', {'id': id})
        self.assertEqual(res['data']['status'], p['status'])

    def test_user_message_read(self):
        p = {
            'title': self.randhan(8),
            'content': self.randhan(64),
        }
        res = self.c.open('/api/my-user-message/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/my-user-message/read', {'id': id})
        self.assertEqual(res['data']['message_obj']['title'], p['title'])
        self.assertEqual(res['data']['message_obj']['content'], p['content'])



test_list = [
    'test_user_message_list',
    'test_user_message_create',
    'test_user_message_delete',
    'test_user_message_update',
    'test_user_message_read',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



